﻿using InvestmentIntelligence.DbModel.Models;
using System;
using System.Collections.Generic;
using System.Linq;

namespace InvestmentIntelligence.Data.Repository.EFImplementation
{
    using System.Data.Entity;

    public class FxSerieRepository : IIPRepository<FxSerie>, IFxSerieUpdatableRepository
    {
        public List<T> GetList<T>(Func<IQueryable<FxSerie>, IEnumerable<T>> f, int currencyId)
        {
            return GetList(dc => f(dc.Include("Currency").Where(x => x.CurrencyId == currencyId)));
        }

        public FxSerie Get(int currencyId, DateTime date)
        {
            var tDate = date.Date;
            return Get(dc => dc.Include("Currency").SingleOrDefault(x => x.CurrencyId == currencyId && x.Date == tDate));
        }

        public decimal? GetLast(int currencyId)
        {
            return Get(dc => dc.Include("Currency").Where(c => c.CurrencyId == currencyId).OrderByDescending(x => x.Date).Select(c=>c.FxRateInUsd).FirstOrDefault());
        }

        public DateTime? GetLastDate(int key)
        {
            return Get(v => v.Where(c => c.CurrencyId == key).Max(c => (DateTime?) c.Date));
        }
    }
}